package cn.edu.sdjzu.xg.bysj.service;


import cn.edu.sdjzu.xg.bysj.dao.TeacherDao;
import cn.edu.sdjzu.xg.bysj.domain.*;
import cn.edu.sdjzu.xg.bysj.exception.BysjException;
import cn.edu.sdjzu.xg.bysj.security.Actor;
import cn.edu.sdjzu.xg.bysj.security.ActorService;
import cn.edu.sdjzu.xg.bysj.security.UserDao;
import cn.edu.sdjzu.xg.bysj.util.Condition;
import cn.edu.sdjzu.xg.bysj.util.JdbcHelper;
import cn.edu.sdjzu.xg.bysj.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class TeacherService extends Actor {
    private static final TeacherDao teacherDao = TeacherDao.getInstance();
	private static final TeacherService teacherService = new TeacherService();

    private TeacherService() {
    }

    public static TeacherService getInstance() {
        return teacherService;
    }

    public Collection<Teacher> findAll() throws SQLException, BysjException {
        return this.findAll(null, null);
    }

	public Collection<Teacher> findAll(List<Condition> conditionList_json_str, Pagination pagination)
			throws SQLException, BysjException {
		//获得连接以便完成可能的事务操作
		Connection connection = JdbcHelper.getConn();
		Collection<Teacher> teachers = teacherDao.findAll(connection, conditionList_json_str, pagination);
		JdbcHelper.close(connection);
		return teachers;
	}

	public Teacher find(Integer id) throws SQLException, BysjException {
		//获得connection资源
		Connection connection = JdbcHelper.getConn();
		Teacher teacher = null;
		teacher = teacherDao.find(connection, id);
		//关闭connection资源
		JdbcHelper.close(connection);
		return teacher;
	}
//
//	public boolean update(Teacher teacher) throws SQLException {
//		return teacherDao.update(teacher);
//	}
//
//	public boolean delete(Integer id) throws SQLException, BysjException {
//		Teacher teacher = this.find(id);
//		return teacherDao.delete(teacher);
//	}

//	public boolean delete(Teacher teacher) throws SQLException {
//		//如果该教师有关联的课题，不能删除
////		if(teacher.getProjects().size()>0){
////			return false;
////		}
//		return teacherDao.delete(teacher);
//	}

	/**
	 * 一些方法使用了事务，所以即使仅测试dao也应从Service中开始。
	 *
	 * @param args
	 * @throws SQLException
	 */
	public static void main(String[] args) throws SQLException, BysjException {
		Connection connection = JdbcHelper.getConn();
//		ProfTitle profTitle = ProfTitleService.getInstance().find(1);
		Department department = DepartmentService.getInstance().find(1);
//		Degree degree = DegreeService.getInstance().find(1);
		Teacher t1 = new Teacher(1,"04", "name04", "教授", department);
		System.out.println(t1);
//		TeacherService.getInstance().add(t1);

	}

	/**
	 * 添加Teacher对象
//	 * @param teacher 要添加的Teacher对象
	 * @return 添加成功返回true，否则false
	 * @throws SQLException
	 */
	public boolean add(Teacher teacher) throws SQLException {
		//获得进行事务的连接，添加教师和添加用户都使用本连接对象
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		boolean added = false;
		try {
            //添加actor对象，返回值为自动生成的id
            int actorId = ActorService.getInstance().add(connection);
            //teacher的id取actor的id
            teacher.setId(actorId);
            //teacherDao添加教师对象，无返回值
//            teacherDao.add(teacher, connection);
            //创建用户对象，密码和用户名取自teacher字段值no
            User user = new User(0, teacher.getNo(), teacher.getNo(), teacher);
            //增加用户
            added = UserDao.getInstance().add(user, connection);
            //提交事务
            connection.commit();
        } catch (SQLException e) {
			//回滚事务中所有操作
			e.printStackTrace();
			connection.rollback();
		} finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(null, connection);
		}
		return added;
	}

	@Override
	public Integer getId() {
		return null;
	}

	@Override
	public void setId(Integer id) {

	}

	@Override
	public String getName() {
		return null;
	}

	@Override
	public void setName(String name) {

	}

	@Override
	public String getNo() {
		return null;
	}

	@Override
	public void setNo(String no) {

	}
}
